package cn.lopr.im.android.mvp.view.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;
import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.User;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author shuangyang7
 * @date 2021/1/6 9:22
 */
public class AddFriendAdapter extends RecyclerView.Adapter<AddFriendAdapter.ViewHolder> {

    private List<User> users;
    private Context context;
    private OnItemListen mOnItemListen;

    private String action = "search";
    private String currentAction;

    public AddFriendAdapter(Context context, String currentAction) {
        this.context = context;
        this.currentAction = currentAction;
        this.users = new ArrayList<>();
    }

    public AddFriendAdapter(Context context, List<User> users) {
        this.users = users;
        this.context = context;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void refer(List<User> users) {
        this.users = users;
        if (this.users == null) {
            this.users = new ArrayList<>();
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate
                (R.layout.list_item_add_friend, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        User user = this.users.get(position);

        if (mOnItemListen != null) {
            holder.tvSend.setTag(user);
            holder.tvSend.setOnClickListener(e -> mOnItemListen.onClick(e, position));
        }
        if (!action.equals(currentAction)) {
            holder.tvSend.setText("同意");
        }
        if (user.isApply()) {
            holder.tvSend.setText("以发送");
            holder.tvSend.setBackground(null);
        }
        holder.tvNickname.setText(String.format("%s (%s)", user.getNickname(), user.getPhone()));
        holder.tvMsg.setText(user.getIntroduction());

        Glide.with(context).load(user.getPortrait())
                .placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        holder.ivPortrait.setImageDrawable(errorDrawable);
                    }
                });

    }

    public void setOnItemListen(OnItemListen onItemListen) {
        this.mOnItemListen = onItemListen;
    }

    public interface OnItemListen {
        void onClick(View view, int i);

        void onLongClick(View view, int i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout llContent;
        CircleImageView ivPortrait;
        TextView tvNickname;
        TextView tvMsg;
        TextView tvSend;

        public ViewHolder(View itemView) {
            super(itemView);
            llContent = (LinearLayout) itemView.findViewById(R.id.ll_content);
            ivPortrait = (CircleImageView) itemView.findViewById(R.id.iv_portrait);
            tvNickname = (TextView) itemView.findViewById(R.id.tv_nickname);
            tvMsg = (TextView) itemView.findViewById(R.id.tv_msg);
            tvSend = (TextView) itemView.findViewById(R.id.tv_send);
        }
    }
}
